package com.sgcc.activiti.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sgcc.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.activiti.mapper.OAnomalyTypeDurationMapper;
import com.sgcc.activiti.domain.OAnomalyTypeDuration;
import com.sgcc.activiti.service.IOAnomalyTypeDurationService;

/**
 * OAnomalyTypeDurationService业务层处理
 * 
 * @author sgcc
 * @date 2021-04-09
 */
@Service
public class OAnomalyTypeDurationServiceImpl implements IOAnomalyTypeDurationService 
{
    @Autowired
    private OAnomalyTypeDurationMapper oAnomalyTypeDurationMapper;

    /**
     * 查询OAnomalyTypeDuration
     * 
     * @param id OAnomalyTypeDurationID
     * @return OAnomalyTypeDuration
     */
    @Override
    public OAnomalyTypeDuration selectOAnomalyTypeDurationById(Integer id)
    {
        return oAnomalyTypeDurationMapper.selectOAnomalyTypeDurationById(id);
    }

    /**
     * 查询OAnomalyTypeDuration列表
     * 
     * @param oAnomalyTypeDuration OAnomalyTypeDuration
     * @return OAnomalyTypeDuration
     */
    @Override
    public List<OAnomalyTypeDuration> selectOAnomalyTypeDurationList(OAnomalyTypeDuration oAnomalyTypeDuration)
    {
        return oAnomalyTypeDurationMapper.selectOAnomalyTypeDurationList(oAnomalyTypeDuration);
    }

    /**
     * 新增OAnomalyTypeDuration
     * 
     * @param oAnomalyTypeDuration OAnomalyTypeDuration
     * @return 结果
     */
    @Override
    public int insertOAnomalyTypeDuration(OAnomalyTypeDuration oAnomalyTypeDuration)
    {
        oAnomalyTypeDuration.setCreateTime(DateUtils.getNowDate());
        return oAnomalyTypeDurationMapper.insertOAnomalyTypeDuration(oAnomalyTypeDuration);
    }

    /**
     * 修改OAnomalyTypeDuration
     * 
     * @param oAnomalyTypeDurationList
     * @return 结果
     */
    @Override
    public int updateOAnomalyTypeDuration(List<OAnomalyTypeDuration> oAnomalyTypeDurationList) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] json = objectMapper.writeValueAsBytes(oAnomalyTypeDurationList);
        OAnomalyTypeDuration[] oAnomalyTypeDurations = objectMapper.readValue(json, OAnomalyTypeDuration[].class);
        List<OAnomalyTypeDuration> oatdList = new ArrayList<>();
        Collections.addAll(oatdList, oAnomalyTypeDurations);
        for(OAnomalyTypeDuration oAnomalyTypeDuration : oatdList){
            oAnomalyTypeDuration.setUpdateTime(DateUtils.getNowDate());
            oAnomalyTypeDurationMapper.updateOAnomalyTypeDuration(oAnomalyTypeDuration);
        }
        return 1;
    }

    /**
     * 批量删除OAnomalyTypeDuration
     * 
     * @param ids 需要删除的OAnomalyTypeDurationID
     * @return 结果
     */
    @Override
    public int deleteOAnomalyTypeDurationByIds(Integer[] ids)
    {
        return oAnomalyTypeDurationMapper.deleteOAnomalyTypeDurationByIds(ids);
    }

    /**
     * 删除OAnomalyTypeDuration信息
     * 
     * @param id OAnomalyTypeDurationID
     * @return 结果
     */
    @Override
    public int deleteOAnomalyTypeDurationById(Integer id)
    {
        return oAnomalyTypeDurationMapper.deleteOAnomalyTypeDurationById(id);
    }
}
